.PHONY:all clean r ci cir up rde rds rcom d g bochs_debug gdb_debug od rb gr dg

OBJDIR = obj
TARGET = $(OBJDIR)/a.img
KERNEL_BIN = obj/kernel.bin

SRCDIR += .
SRCDIR += src
SRCDIR += src/kernel
SRCDIR += src/lib
SRCDIR += src/proc
SRCDIR += src/test
SRCDIR += src/kernel/tty
SRCDIR += src/driver
SRCDIR += src/fs

SRCC = $(foreach i, $(SRCDIR), $(wildcard $(i)/*.c))
OBJS_C = $(foreach i, $(SRCC), obj/$(i:.c=.o))
OBJS_ASM = obj/src/kernel/kernel.o obj/src/lib/sub.o obj/src/kernel/syscall.o
OUTDIRS = $(foreach i, $(dir $(SRCC)), obj/$(i))
INCLUDE1 = $(foreach i, $(SRCDIR), src/include/$(subst src,,$(i)))
INCLUDE = $(subst //,/,$(INCLUDE1))
INCLUDE += $(SRCDIR)
IINCLUDE = $(foreach i, $(INCLUDE), -I$(i))
C_FLAGS = -c $(IINCLUDE)


CIM=

C_FLAGS += -fno-stack-protector
C_FLAGS += -fno-builtin #close inter-replace by gcc, such as print -> puts
C_FLAGS += -Wall
C_FLAGS += -ggdb
C_FLAGS += -O0


LD_FLAGS = -nostartfiles 
LD_FLAGS += -no-stdlib #do not use any lib and start-file(such as bengin.o) from gcc, it include -nostartfile option
LD_FLAGS += -O0
LD_FLAGS += -m elf_i386

# dd if=./obj/boot.o of=obj/a.img bs=512 count=1 conv=notrunc
all:premake tool/bin/os_tool $(KERNEL_BIN)
	dd if=/dev/zero of=obj/a.img bs=1k count=1440
	nasm src/boot/boot.asm -o ./obj/boot.o -l ./obj/boot.map
	./tool/bin/os_tool -add_sect ./obj/boot.o ./obj/a.img 0
	nasm src/boot/loader.asm -o ./obj/loader.o -l ./obj/loader.map
	./tool/bin/os_tool -add_sect ./obj/loader.o ./obj/a.img 1
	./tool/bin/os_tool -add_sect ./obj/kernel.bin ./obj/a.img 121

premake:
	@echo make premake...
	$(foreach i, $(OUTDIRS), $(shell mkdir -p $(i)))

$(KERNEL_BIN):$(OBJS_ASM) $(OBJS_C)
	ld -s -Ttext 0x30400 -o $@ $^
	ld -Ttext 0x30400 -o $@.g $^

$(OBJS_ASM):src/kernel/kernel.asm src/lib/sub.asm
	nasm -f elf src/kernel/kernel.asm -o obj/src/kernel/kernel.o
	nasm -f elf src/kernel/syscall.asm -o obj/src/kernel/syscall.o
	nasm -f elf src/lib/sub.asm -o obj/src/lib/sub.o

obj/%.o:%.c
	gcc $(C_FLAGS) $< -o $@

tool/bin/os_tool:
	make -C tool
tool/bin/f12t:
	make -C tool

#-bochs -qf img/bochsrc_naked
r:all
	test -e img/hd.img || tar xzvf img/hd.img.tar.gz -C img/
	-bochs -qf img/bochsrc_naked
rb:all
	test -e img/hd.img || tar xzvf img/hd.img.tar.gz -C img/
	-bochs -qf img/bochsrc_naked_disable_gdb

# target remote localhost:1234
g:
	gdb ./obj/kernel.bin.g

rde: #run freedos-edit
	test -e img/c.img || tar xzvf img/freedos_img.tar.gz -C img/
	-bochs -qf img/bochsrc_with_edit
rds: #run freedos_simp
	test -e img/freedos_simp.img || tar xzvf img/freedos_img.tar.gz -C img/
	-bochs -qf img/bochsrc_simp
rcom:tool/bin/os_tool all #run com
	./tool/bin/f12t -c img/b.img
	test -d /mnt/floppy || sudo mkdir -t /mnt/floppy
	-sudo umount /mnt/floppy
	sudo mount -o loop img/b.img /mnt/floppy
	sudo cp obj/boot.o /mnt/floppy/ 
	sudo umount /mnt/floppy
	make rds

rst: #read sectors test
	make all
	./tool/bin/os_tool -t obj/a.img
	bochs -qf img/bochsrc_naked

clean:
	make -C tool clean
	@-rm -rf ./obj $(TARGET)
	@echo clean done.

ci:
	git add -A
	git status
	git commit -m "$(CIM)"
cir:
	git push origin master
up:
	git pull origin
fdl:
	make -C tool
	test -e img/hd.img || tar xzvf img/hd.img.tar.gz -C img/
	./tool/bin/os_tool -fdl img/hd.img
	dot -Tpng memo/hd.dot -o memo/hd.png

# bochs download: https://sourceforge.net/projects/bochs/files/bochs/2.6.8/
# sudo apt-get install libsdl2-dev
bochs_debug:
	cd ~/soft/bochs-2.6.8 && ./configure --with-sdl2  --enable-disasm --enable-debugger && make && sudo make install && cd -
	
gdb_debug:
	cd ~/soft/bochs-2.6.8 && ./configure --with-sdl2  --enable-disasm --enable-gdb-stub && make && sudo make install && cd -

od:
	objdump -DS obj/kernel.bin.g | less
gr:
	dot -Tpng memo/gr.dot -o memo/gr.png
dg:
	display memo/gr.png
d:
	@echo 'OBJS_C=$(OBJS_C)'
	@echo 'SRCC = $(SRCC)'
	@echo 'INCLUDE=$(INCLUDE)'

help:
	@echo 'when debug'
	@echo 'open 1 conosle, enter the "myos_onx86" directory.' 
	@echo 'input "make r" command'
	@echo 'this time "Waiting for gdb connection on port 1234" should showing.'
	@echo
	@echo 'then open another console, also enter the "myos_onx86" directory.'
	@echo 'input "make g" command'
	@echo 'input "target remote localhost:1234"'
	@echo '(or "krg" if gdb.init script was configed.)'
	@echo 'input "r" to run os.'
#for usb start.	
#sudo dd if=/dev/sdb1 of=ToshibaUsb_sector1_orig.bin bs=512 count=1 conv=notrunc
#sudo dd of=/dev/sdb1 if=../obj/boot.o bs=512 count=1 conv=notrunc

#copy a.img(floppy image to usb)
ubt:all
	sudo dd if=obj/a.img of=/dev/sdb bs=1 count=1474560

#sudo hexdump /dev/sdb -C|less
